package com.stylefeng.guns.modular.front.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.constant.cache.Cache;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.core.base.controller.FrontBaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.exception.FrontException;
import com.stylefeng.guns.modular.flagglory.service.*;
import com.stylefeng.guns.modular.flagglory.service.impl.ReadRecordServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/2/7
 * Time: 14:44
 */
@RestController
@RequestMapping("/front/book")
public class FrontBookController extends FrontBaseController {
    @Autowired
    private IBookService bookService;
    @Autowired
    private IChapterService chapterService;
    @Autowired
    private IReadRecordService readRecordService;
    @Autowired
    private ISettingService settingService;

    @RequestMapping(value = "/getAllBooks", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部目录")
    public Object getAllBooks() throws FrontException {
        EntityWrapper<Book> bookEntityWrapper = new EntityWrapper<>();
        bookEntityWrapper.orderBy("sort");
        List<Book> books = bookService.selectList(bookEntityWrapper);
        JSONObject result = new JSONObject();
        result.put("books", books);
        return new SuccessTip(result);
    }

    @RequestMapping(value = "/getChaptersByBookId", method = RequestMethod.GET)
    @ApiOperation("根据目录id获取全部章节")
    @ApiImplicitParam(name = "bookId", value = "目录id", dataType = "int", paramType = "query", defaultValue = "2")
    public Object getChaptersByBookId(Integer bookId) throws FrontException {
        Book book = bookService.selectById(bookId);
        if (null == book) {
            throw new FrontException("该目录不存在");
        }
        List<Map<String, Object>> chapters = bookService.selectChaptersByBookId(bookId);
        JSONObject result = new JSONObject();
        result.put("chapters", chapters);
        result.put("book", book);
        return new SuccessTip(result);
    }

    @RequestMapping(value = "/getChapter", method = RequestMethod.GET)
    @ApiOperation("根据章节id获取章节")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "章节id", dataType = "int", paramType = "query", defaultValue = "3"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", paramType = "query", defaultValue = "9")})
    public Object getChapter(Integer id, Integer userId) throws FrontException {
        Chapter chapter = chapterService.selectById(id);
        if (null == chapter) {
            throw new FrontException("该章节不存在");
        }
        Integer sort = chapter.getSort();
        Map<String, Object> nextChapter = bookService.selectNextOrBeforeChapter(true, sort, chapter.getBookid());
        Map<String, Object> beforeChapter = bookService.selectNextOrBeforeChapter(false, sort, chapter.getBookid());
        JSONObject result = new JSONObject();
        result.put("chapter", chapter);
        result.put("beforeChapter", beforeChapter);
        result.put("nextChapter", nextChapter);
        try {
            chapterService.updateRecord(chapter, userId, result);
        } catch (FrontException e) {
            result.put("getFlag", 0);
            e.printStackTrace();
        }
        return new SuccessTip(result);
    }

    @RequestMapping(value = "/getReadRecord", method = RequestMethod.GET)
    @ApiOperation("获取今日阅读记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", paramType = "query", defaultValue = "9")})
    public Object getReadRecord(Integer userId) throws FrontException {
        EntityWrapper<ReadRecord> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("userid=" + userId).and("to_days(createtime)=to_days(now())");
        int count = readRecordService.selectCount(entityWrapper);
        JSONObject result = new JSONObject();
        Setting readFlagSetting = settingService.getSetting("read_flag");
        Setting maxReadSetting = settingService.getSetting("max_read");
        int readFlag = Integer.parseInt(readFlagSetting.getValue());
        int maxRead = Integer.parseInt(maxReadSetting.getValue());
        if (count > maxRead) {
            count = maxRead;
        }
        result.put("readCount", count);
        result.put("maxRead", maxRead);
        result.put("getFlag", count * readFlag);
        result.put("maxFlag", maxRead * readFlag);
        return new SuccessTip(result);
    }
}
